package com.durian.base.frame.titlebar;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.durian.base.frame.activity.ActivityFrame;

/**
 * 说明：TitleBarSupport
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2020-01-07 18:52
 * <p/>
 * 版本：version 1.0
 */
public class TitleBarSupport {

    private View titleView;

    public void bind(ActivityFrame activity,ISuperTitleBar titleBar,int rootViewResId){
        final LayoutInflater inflater = LayoutInflater.from(activity);
        final View userView = inflater.inflate(rootViewResId,null);
        bind(activity,titleBar,userView);
    }

    public void bind(ActivityFrame activity,ISuperTitleBar titleBar,View userView){
        titleBar.bindActivity(activity);
        titleView = titleBar.createTitleBar(activity);
        if (titleView != null){
            final LinearLayout contentView = new LinearLayout(activity);
            contentView.setOrientation(LinearLayout.VERTICAL);
            final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            contentView.setLayoutParams(params);
            titleView = titleBar.createTitleBar(activity);
            if (titleView != null){
                contentView.addView(titleView);
            }

            final ViewGroup.LayoutParams userParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
            userView.setLayoutParams(userParams);
            contentView.addView(userView);
            activity.setContentView(contentView);

            if (titleView != null){
                titleBar.bind(activity,titleView);
            }
        }else {
            activity.setContentView(userView);
        }
    }

    public void unBind(){
        titleView = null;
    }

    public View getTitleView(){
        return titleView;
    }
}
